package com.chhuang.core.bean;

import java.io.Serializable;
import java.util.List;

import com.chhuang.core.model.Persistable;

/**
 * 
 * @author 黄晨
 *
 * @param <TDo>
 */
public abstract class QueryBean<TPo extends Persistable> implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2844826560156436126L;
	
	private List<TPo> result;
	/**
	 * 页码 >0
	 */
	private int pageNum;
	/**
	 * 第页几条记录
	 */
	private int pageSize;
	/**
	 * 一共多少页
	 */
	private int pageCount;
	/**
	 * 一共多少条记录
	 */
	private int itemCount;
	/**
	 * 本次查询是从数据库中第几条记录开始，>=0
	 */
	private int offset;
	/**
	 * 本次查询查多少条记录，与pageSize相等
	 */
	private int limit;
	/**
	 * 用于Oracle数据库，表示起始行
	 */
	private int rownumf;
	/**
	 * 用于Oracle数据库，表示结束行
	 */
	private int rownumt;
	/**
	 * 查询排序按哪个字段排序，数据库表字段名
	 */
	private String orderBy;
	/**
	 * 查询排序按哪个字段排序，实体类的属性名
	 */
	private String sort;
	/**
	 * 排序方法字符串desc 或者 asc
	 */
	private String order;
	/**
	 * 是否从小到大排序
	 */
	private boolean asc=true;
	/**
	 * 是否从大到小排序
	 */
	private boolean desc=false;
	/**
	 * 连接查询条件的方法是否是and
	 */
	private boolean and=true;
	/**
	 * 连接查询条件的方法是否是or
	 */
	private boolean or=false;
	public List<TPo> getResult() {
		return result;
	}
	public void setResult(List<TPo> result) {
		this.result = result;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
		this.offset = (pageNum-1)*this.pageSize;
		this.rownumf = this.offset+1;
		this.rownumt = this.offset+this.pageSize;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		this.limit = pageSize;
		this.offset = (this.pageNum-1)*pageSize;
		this.rownumf = this.offset+1;
		this.rownumt = this.offset+pageSize;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getItemCount() {
		return itemCount;
	}
	public void setItemCount(int itemCount) {
		this.itemCount = itemCount;
	}
	public int getOffset() {
		return offset;
	}
	public void setOffset(int offset) {
		this.offset = offset;
		this.pageNum = (offset/this.limit)+1;
		this.rownumf = offset+1;
		this.rownumt = offset+this.limit;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
		this.pageSize = limit;
		this.pageNum = (this.offset/limit)+1;
		this.rownumt = this.offset+limit;
	}
	public int getRownumf() {
		return rownumf;
	}
	public void setRownumf(int rownumf) {
		this.rownumf = rownumf;
	}
	public int getRownumt() {
		return rownumt;
	}
	public void setRownumt(int rownumt) {
		this.rownumt = rownumt;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
		this.sort = this.columnTransforField(orderBy);
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
		this.orderBy = this.fieldTransforColumn(sort);
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
		if("desc".equalsIgnoreCase(order)){
			this.desc = true;
			this.asc = false;
		}else{
			this.desc = false;
			this.asc = true;
		}
	}
	public boolean isAsc() {
		return asc;
	}
	public void setAsc(boolean asc) {
		this.asc = asc;
		this.desc=!asc;
		if(asc){
			this.order = "asc";
		}else{
			this.order = "desc";
		}
	}
	public boolean isDesc() {
		return desc;
	}
	public void setDesc(boolean desc) {
		this.desc = desc;
		this.asc=!desc;
		if(desc){
			this.order = "desc";
		}else{
			this.order = "asc";
		}
	}
	public boolean isAnd() {
		return and;
	}
	public void setAnd(boolean and) {
		this.and = and;
		this.or = !and;
	}
	public boolean isOr() {
		return or;
	}
	public void setOr(boolean or) {
		this.or = or;
		this.and = !or;
	}
	
	/**
	 * 把实体类的对象属性名转换成数据库表字段名
	 * @param field
	 * @return
	 */
	public abstract String fieldTransforColumn(String field);
	
	/**
	 * 把数据库表字段名转换成实体类的对象属性名
	 * @param field
	 * @return
	 */
	public abstract String columnTransforField(String column);
}
